-- 运营活动:拼单抢购


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 64, -- 活动id (64 拼单抢购)
    act_name = "拼单抢购", -- 活动名称
    act_desc = "参与团购！你们的购买数量只要达到指定数量即可领取奖励！", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1693267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        giftBagList = 
        {
            {
                giftBagType = 1,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
            {
                giftBagType = 2,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
            {
                giftBagType = 3,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
            {
                giftBagType = 4,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
            {
                giftBagType = 5,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
            {
                giftBagType = 6,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
            {
                giftBagType = 7,  --礼包类型，1-n
                giftBagName = "", --礼包名字
                giftBagNeedGoldNum = 3000,  --原价
                Awards = 
                {       
                    { type = 0, id = 518, count = 1, bind = 1, },
                    { type = 0, id = 564, count = 3, bind = 1, },
                },
                precentBack = 
                {
                    {
                        playerNum = 1,  --拼单人数
                        discount = 0,   --折扣
                        disCountNum = 0,   --返还元宝数量
                    },
                    {
                        playerNum = 2,
                        discount = 9,
                        disCountNum = 100,
                    },
                    {
                        playerNum = 3,
                        discount = 7,
                        disCountNum = 300,
                    },
                    {
                        playerNum = 4,
                        discount = 5,
                        disCountNum = 500,
                    },
                },
            },
        },
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_64_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.giftBagList == nil then return 23 end
    if type(config.giftBagList) ~= "table" then return 24 end
    for idx = 1, #config.giftBagList do
        local dayRewards = config.giftBagList[idx]
        if dayRewards == nil then return 25 end
        if type(dayRewards) ~= "table" then return 26 end
        local newDayRewards = dayRewards.Awards
        if newDayRewards == nil then return 27 end
        if type(newDayRewards) ~= "table" then return 28 end
        if not oper_act_check_one_award_cfg(newDayRewards) then
            return 22
        end 
    end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_64_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_64_unsetup(cfg)   
end

-- 活动开始
function oper_act_64_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_64_start = 1
    ssVar.oper_act_64_cmd = cfg.cmd_id
    ssVar.oper_act_64_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_OPERATION_64_PLAYER, 1)

    local needClearState = true

    local crossDayState = false

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            needClearState = false
            local time = System.getSystemTime()
            if myData and myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
                crossDayState = true
            end
        end
    end

    if crossDayState then
        if cfg then
            local config = cfg.config
            if config then 
                local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_ORDERS].strName)
                local rankCount = Ranking.getRankItemCount(rankPtr)
                for idx = 1, rankCount do
                    local item = Ranking.getItemFromIndex(rankPtr, idx-1)
                    if item then
                        local orderName = Ranking.getSub(item, 0)
                        local data = Ranking.getSub(item, 1)
                        local dataTable
                        if not data or data == "" then
                            dataTable = {}
                        else
                            dataTable = BaseFuc_unserialize(data)
                        end
                      
                        if dataTable.playerList and dataTable.orderType then
                            local orderConfig = oper_act_64_get_order_config(config, dataTable.orderType)
                            if orderConfig then
                                local playerNum = #dataTable.playerList
                                local discountConfig = oper_act_64_get_discount_config(orderConfig, playerNum)
                                if discountConfig then
                                    for ii = 1, playerNum do
                                        local actorid = dataTable.playerList[ii]
                                        local playerData = gRankMgr_GetValueById(actorid, RANK_OPERATION_64_PLAYER, 1)
                                        local myPlayerData = BaseFuc_unserialize(playerData)
                                        if not myPlayerData then
                                            myPlayerData = {}
                                        end

                                        local rewards = orderConfig.Awards
                                        local title = Lang.ScriptTips.Operation174
                                        local content = Lang.ScriptTips.Operation175
                                        Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId064, Lang.LogTips.Operation064, myPlayerData.vocation, myPlayerData.sex)


                                        if discountConfig.disCountNum and discountConfig.disCountNum > 0 then
                                            local rewards = {{type = qatYuanbao, id = 0, count = discountConfig.disCountNum}}
                                            local title = Lang.ScriptTips.Operation176
                                            local content = Lang.ScriptTips.Operation177
                                            Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId064, Lang.LogTips.Operation064, myPlayerData.vocation, myPlayerData.sex)
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end

    if needClearState or crossDayState then
        gRankMgr_Clear(RANK_OPERATION_64_PLAYER)
        gRankMgr_Clear(RANK_OPERATION_64_ORDERS)

        ssVar.oper_act_64_cur_order = {}
    end
end

-- 活动结束
function oper_act_64_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_64_start = nil
    ssVar.oper_act_64_cmd = nil
    ssVar.oper_act_64_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end

    local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_ORDERS].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local orderName = Ranking.getSub(item, 0)
            local data = Ranking.getSub(item, 1)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data)
            end
          
            if dataTable.playerList and dataTable.orderType then
                local orderConfig = oper_act_64_get_order_config(config, dataTable.orderType)
                if orderConfig then
                    local playerNum = #dataTable.playerList
                    local discountConfig = oper_act_64_get_discount_config(orderConfig, playerNum)
                    if discountConfig then
                        for ii = 1, playerNum do
                            local actorid = dataTable.playerList[ii]

                            local playerData = gRankMgr_GetValueById(actorid, RANK_OPERATION_64_PLAYER, 1)
                            local myPlayerData = BaseFuc_unserialize(playerData)
                            if not myPlayerData then
                                myPlayerData = {}
                            end

                            local rewards = orderConfig.Awards
                            local title = Lang.ScriptTips.Operation174
                            local content = Lang.ScriptTips.Operation175
                            Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId064, Lang.LogTips.Operation064, myPlayerData.vocation, myPlayerData.sex)


                            if discountConfig.disCountNum and discountConfig.disCountNum > 0 then
                                local rewards = {{type = qatYuanbao, id = 0, count = discountConfig.disCountNum}}
                                local title = Lang.ScriptTips.Operation176
                                local content = Lang.ScriptTips.Operation177
                                Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId064, Lang.LogTips.Operation064, myPlayerData.vocation, myPlayerData.sex)
                            end
                        end
                    end
                end
            end
        end
    end
end

-- 打包数据
function oper_act_64_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_64_start then
        return
    end
    if not cfg then return end
    local config = cfg.config
    if not config then return end

    if not ssVar.oper_act_64_cur_order then
        ssVar.oper_act_64_cur_order = {}
    end
    local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_PLAYER].strName)

    if not rankPtr then
        return
    end

    local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (pack == nil) then
        return
    end

    local myActorId = Actor.getActorId(sysarg)
    local myCurOrderNum = 0

    local curOrderNum = 0
    for idx = 1, #config.giftBagList do
        local orderType = config.giftBagList[idx].giftBagType
        if ssVar.oper_act_64_cur_order and ssVar.oper_act_64_cur_order[orderType] then
            curOrderNum = curOrderNum + 1
        end
    end
    --print("curOrderNum ="..curOrderNum)
    DataPack.writeByte(pack, curOrderNum)
    if curOrderNum > 0 then
        for idx = 1, #config.giftBagList do
            local orderType = config.giftBagList[idx].giftBagType
            --local orderName = config.giftBagList[idx].giftBagName
            local orderId = ssVar.oper_act_64_cur_order[orderType]
            if orderId then
                local orderData = gRankMgr_GetValueById(orderId, RANK_OPERATION_64_ORDERS, 1)
                local curOrderData = BaseFuc_unserialize(orderData) or {}
                DataPack.writeInt(pack, orderId)
                --print("orderId ="..orderId)
                DataPack.writeByte(pack, curOrderData.orderType or 0)
                --print("curOrderData.orderType or 0 ="..(curOrderData.orderType or 0))
                local playerNum = 0
                if curOrderData.playerList then
                    playerNum = #curOrderData.playerList
                end
                --print("playerNum ="..playerNum)
                DataPack.writeByte(pack, playerNum)
                if playerNum > 0 then
                    for ii = 1, playerNum do
                        local actorid = curOrderData.playerList[ii]
                        local playerItem = Ranking.getItemPtrFromId(rankPtr, actorid)
                        if playerItem then
                            if myActorId == actorid then
                                myCurOrderNum =  myCurOrderNum + 1
                            end
                            local playerName = Ranking.getSub(playerItem, 0)
                            local playerData = Ranking.getSub(playerItem, 1)
                            local PlayerContentData = BaseFuc_unserialize(playerData)
                            if not PlayerContentData then
                                PlayerContentData = {}
                            end
                            --print("actorid ="..actorid)
                            DataPack.writeInt(pack, actorid or 0)
                            --print("playerName ="..(playerName or ""))
                            DataPack.writeString(pack, playerName or "")
                            --print("PlayerContentData.vocation ="..(PlayerContentData.vocation or 0))
                            DataPack.writeByte(pack, PlayerContentData.vocation or 0)
                            --print("PlayerContentData.sex ="..(PlayerContentData.sex or 0))
                            DataPack.writeByte(pack, PlayerContentData.sex or 0)
                            local state = 0
                            if curOrderData.startPlayerId and curOrderData.startPlayerId == actorid then
                                state = 1
                            end
                            --print("state ="..state)
                            DataPack.writeByte(pack, state)
                        end
                    end
                end
            end
        end
    end
                            

    
    local myPlayerData = gRankMgr_GetValueById(myActorId, RANK_OPERATION_64_PLAYER, 1)

    local myData = BaseFuc_unserialize(myPlayerData)
    

    if not myData then
        myData = {}
    end

    --[[
    if not myData.myOrderList then
        myData.myOrderList = {}
    end

    local myOrderNum = #myData.myOrderList

    ]]

    local myOrderList = oper_act_64_get_my_order_list(sysarg, config)

    local myOrderNum = #myOrderList

    local myFinishOrderNum = myOrderNum - myCurOrderNum
    if myFinishOrderNum < 0 then
        myFinishOrderNum = 0
    end

    DataPack.writeByte(pack, myFinishOrderNum)
    --print("myFinishOrderNum ="..myFinishOrderNum)

    if myFinishOrderNum > 0 then
        for idx = 1, myOrderNum do
            --local myOrderId = myData.myOrderList[idx]
            local myOrderId = myOrderList[idx]
            --print("myOrderId ="..myOrderId)
            if myOrderId then
                local myOrderData = gRankMgr_GetValueById(myOrderId, RANK_OPERATION_64_ORDERS, 1)
                local finishOrderData = BaseFuc_unserialize(myOrderData) or {}
                local playerNum = 0
                if finishOrderData.playerList then
                    playerNum = #finishOrderData.playerList
                end
                local orderConfig = oper_act_64_get_order_config(config, finishOrderData.orderType or 0)
                if orderConfig and orderConfig.precentBack[#orderConfig.precentBack].playerNum == playerNum then
                    DataPack.writeInt(pack, myOrderId)
                    --print("myOrderId ="..myOrderId)
                    DataPack.writeByte(pack, finishOrderData.orderType or 0)
                    --print("finishOrderData.orderType ="..(finishOrderData.orderType or 0))
                    DataPack.writeByte(pack, playerNum)
                    --print("playerNum ="..playerNum)
                    for ii = 1, playerNum do
                        local actorid = finishOrderData.playerList[ii]
                        local playerItem = Ranking.getItemPtrFromId(rankPtr, actorid)
                        if playerItem then
                            local playerName = Ranking.getSub(playerItem, 0)
                            local playerData = Ranking.getSub(playerItem, 1)
                            local PlayerContentData = BaseFuc_unserialize(playerData)
                            if not PlayerContentData then
                                PlayerContentData = {}
                            end
                            DataPack.writeInt(pack, actorid or 0)
                            --print("actorid ="..actorid)
                            DataPack.writeString(pack, playerName or "")
                            --print("playerName ="..(playerName or ""))
                            DataPack.writeByte(pack, PlayerContentData.vocation or 0)
                            --print("PlayerContentData.vocation ="..(PlayerContentData.vocation or 0))
                            DataPack.writeByte(pack, PlayerContentData.sex or 0)
                            --print("PlayerContentData.sex ="..(PlayerContentData.sex or 0))
                            local state = 0
                            if finishOrderData.startPlayerId and finishOrderData.startPlayerId == actorid then
                                state = 1
                            end
                            DataPack.writeByte(pack, state)
                            --print("state ="..state)
                        end
                    end
                end
            end
        end
    end

    DataPack.flush(pack)
    
    --DataPack.writeByte(netPack, 1)        -- 活动特有参数
end

-- 操作，返回操作结果，0为成功
--[[
 (uchar) 类型 1 发起订单 2邀请加入 3加入
    if(类型 == 1)
    {
        (uchar) 礼包类型
    }
    elseif(类型 == 2)
    {
        (int) 订单id
        (string) 玩家名字
    }
    elseif(类型 == 3)
    {
        (int) 订单id
        (uchar) 加入类型 1广播超链接 2主动加入 3被邀请
    }
]]
function oper_act_64_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_64_do cfg.config confg error")
        return
    end

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_64_cur_order then
        ssVar.oper_act_64_cur_order = {}
    end
    local myActorId = Actor.getActorId(sysarg)
    local time = System.getSystemTime()

    local opType = DataPack.readByte(clientPkt)
    local actorName = Actor.getName(sysarg)
    if opType == 1 then
        local orderType = DataPack.readByte(clientPkt)
        local orderConfig = oper_act_64_get_order_config(config, orderType)
        if not orderConfig then
            return
        end

        if ssVar.oper_act_64_cur_order[orderType] then
            local hasOrderId = ssVar.oper_act_64_cur_order[orderType]
            local hasOrderData = gRankMgr_GetValueById(hasOrderId, RANK_OPERATION_64_ORDERS, 1)
            local curHasOrderData = BaseFuc_unserialize(hasOrderData)
            if not curHasOrderData then
                curHasOrderData = {}
            end
            oper_act_64_packet_result_data(sysarg, cfg, hasOrderId, curHasOrderData)

            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation178, ttFlyTip)
            return
        end

        if Actor.getMoneyCount(sysarg, mtYuanbao) < orderConfig.giftBagNeedGoldNum then
            Actor.sendItemLack( sysarg, qatYuanbao, 0, orderConfig.giftBagNeedGoldNum )
            return
        end

        if not Actor.delMoney( sysarg,mtYuanbao,orderConfig.giftBagNeedGoldNum,LogIds.OperationLogId064,Lang.LogTips.Operation064) then
            return
        end

        local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_ORDERS].strName)
        local rankCount = Ranking.getRankItemCount(rankPtr)
        local orderId = rankCount + 1

        local orderData = gRankMgr_GetValueById(orderId, RANK_OPERATION_64_ORDERS, 1)
        local curOrderData = BaseFuc_unserialize(orderData)
        if not curOrderData then
            curOrderData = {}
        end
        curOrderData.orderType = orderType
        if not curOrderData.playerList then
            curOrderData.playerList = {}
        end
        table.insert(curOrderData.playerList, myActorId)

        curOrderData.startPlayerId = myActorId

        if not curOrderData.cmd_id then
            curOrderData.cmd_id = cfg.cmd_id
            curOrderData.act_id = cfg.act_id
            curOrderData.time = time
        end

        local newOrderData = BaseFuc_serialize(curOrderData)
        gRankMgr_SetRankEx(orderId, RANK_OPERATION_64_ORDERS, 1, newOrderData, orderConfig.giftBagName)

        local playerData = gRankMgr_GetValueById(myActorId, RANK_OPERATION_64_PLAYER, 1)
        local curPlayerData = BaseFuc_unserialize(playerData)
        if not curPlayerData then
            curPlayerData = {}
        end

        --[[
        if not curPlayerData.myOrderList then
            curPlayerData.myOrderList = {}
        end

        table.insert(curPlayerData.myOrderList, orderId)

        ]]

        if not curPlayerData.sex then
            curPlayerData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
            curPlayerData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        end

        if not curPlayerData.cmd_id then
            curPlayerData.cmd_id = cfg.cmd_id
            curPlayerData.act_id = cfg.act_id
            curPlayerData.time = time
        end

        curPlayerData.mySelfTime = time

        local newPlayerData = BaseFuc_serialize(curPlayerData)
        gRankMgr_SetRankEx(myActorId, RANK_OPERATION_64_PLAYER, 1, newPlayerData)

        ssVar.oper_act_64_cur_order[orderType] = orderId
        
        local myInviteLink = string.format(Lang.ScriptTips.Operation180, orderId, actorName, orderConfig.giftBagName, orderConfig.giftBagNeedGoldNum)

        local myInviteString = string.format(Lang.ScriptTips.Operation181, actorName, orderConfig.giftBagName, myInviteLink)
        --print("myInviteString ="..myInviteString)
        System.broadcastTipmsg(myInviteString, ttScreenCenter + ttChatWindow)

        oper_act_64_packet_result_data(sysarg, cfg, orderId, curOrderData)
    elseif opType == 2 then
        local orderId = DataPack.readInt(clientPkt)
        --local playerName = DataPack.readString(clientPkt)
        local playerId = DataPack.readInt(clientPkt)

        --print("orderId ="..orderId)

        local orderData = gRankMgr_GetValueById(orderId, RANK_OPERATION_64_ORDERS, 1)
        local curOrderData = BaseFuc_unserialize(orderData)
        if not curOrderData then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end
        if not curOrderData.orderType then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if curOrderData.orderType <= 0 then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if not ssVar.oper_act_64_cur_order[curOrderData.orderType] then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if ssVar.oper_act_64_cur_order[curOrderData.orderType] ~= orderId then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        local orderConfig = oper_act_64_get_order_config(config, curOrderData.orderType)
        if not orderConfig then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        local player = Actor.getActorById(playerId)
        if not player then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation182, ttFlyTip)
            return
        end

        if not curOrderData.playerList then
            curOrderData.playerList = {}
        end

        local curOrderPlayerNum = #curOrderData.playerList
        if curOrderPlayerNum > 0 then
            for idx = 1, curOrderPlayerNum do
                if curOrderData.playerList[idx] == playerId then
                    Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation183, ttFlyTip)
                    return 
                end
            end
        end

        local pack = oper_act_alloc_invite_player_packet(player, cfg.cmd_id, cfg.act_id)
        if not pack then return end
        

        local myInviteString = string.format(Lang.ScriptTips.Operation180, orderId, actorName, orderConfig.giftBagName, orderConfig.giftBagNeedGoldNum)

        --print("myInviteString ="..myInviteString)

        DataPack.writeString(pack, myInviteString)
        DataPack.flush(pack)
    elseif opType == 3 then
        local orderId = DataPack.readInt(clientPkt)
        local inType = DataPack.readByte(clientPkt)

        local orderData = gRankMgr_GetValueById(orderId, RANK_OPERATION_64_ORDERS, 1)
        local curOrderData = BaseFuc_unserialize(orderData)
        if not curOrderData then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end
        if not curOrderData.orderType then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if curOrderData.orderType <= 0 then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if not ssVar.oper_act_64_cur_order[curOrderData.orderType] then
            local noneOrderData = {}
            noneOrderData.orderType = curOrderData.orderType
            oper_act_64_packet_result_data(sysarg, cfg, 0, noneOrderData)

            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if ssVar.oper_act_64_cur_order[curOrderData.orderType] ~= orderId then
            local hasOrderId = ssVar.oper_act_64_cur_order[curOrderData.orderType]
            local hasOrderData = gRankMgr_GetValueById(hasOrderId, RANK_OPERATION_64_ORDERS, 1)
            local curHasOrderData = BaseFuc_unserialize(hasOrderData)
            if not curHasOrderData then
                curHasOrderData = {}
            end
            oper_act_64_packet_result_data(sysarg, cfg, hasOrderId, curHasOrderData)

            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        local orderConfig = oper_act_64_get_order_config(config, curOrderData.orderType)
        if not orderConfig then
            Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation179, ttFlyTip)
            return
        end

        if not curOrderData.playerList then
            curOrderData.playerList = {}
        end

        local curOrderPlayerNum = #curOrderData.playerList
        if curOrderPlayerNum > 0 then
            for idx = 1, curOrderPlayerNum do
                if curOrderData.playerList[idx] == myActorId then
                    Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation184, ttFlyTip)
                    return 
                end
            end
        end

        if Actor.getMoneyCount(sysarg, mtYuanbao) < orderConfig.giftBagNeedGoldNum then
            Actor.sendItemLack( sysarg, qatYuanbao, 0, orderConfig.giftBagNeedGoldNum )
            return
        end

        if not Actor.delMoney( sysarg,mtYuanbao,orderConfig.giftBagNeedGoldNum,LogIds.OperationLogId064,Lang.LogTips.Operation064) then
            return
        end

        table.insert(curOrderData.playerList, myActorId)

        if not curOrderData.cmd_id then
            curOrderData.cmd_id = cfg.cmd_id
            curOrderData.act_id = cfg.act_id
            curOrderData.time = time
        end

        local newOrderData = BaseFuc_serialize(curOrderData)
        gRankMgr_SetRankEx(orderId, RANK_OPERATION_64_ORDERS, 1, newOrderData, orderConfig.giftBagName)


        local playerData = gRankMgr_GetValueById(myActorId, RANK_OPERATION_64_PLAYER, 1)
        local curPlayerData = BaseFuc_unserialize(playerData)
        if not curPlayerData then
            curPlayerData = {}
        end

        --[[
        if not curPlayerData.myOrderList then
            curPlayerData.myOrderList = {}
        end

        table.insert(curPlayerData.myOrderList, orderId)
        ]]

        if not curPlayerData.sex then
            curPlayerData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
            curPlayerData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        end

        if not curPlayerData.cmd_id then
            curPlayerData.cmd_id = cfg.cmd_id
            curPlayerData.act_id = cfg.act_id
            curPlayerData.time = time
        end

        curPlayerData.mySelfTime = time

        local newPlayerData = BaseFuc_serialize(curPlayerData)
        gRankMgr_SetRankEx(myActorId, RANK_OPERATION_64_PLAYER, 1, newPlayerData)
        
        local playerNum = #curOrderData.playerList
        local isFinishOrder = 0
        if orderConfig.precentBack[#orderConfig.precentBack].playerNum == playerNum then
           ssVar.oper_act_64_cur_order[curOrderData.orderType] = nil 
           isFinishOrder = 1
        end
        for idx = 1, playerNum do
            local actorId = curOrderData.playerList[idx]
            local player = Actor.getActorById(actorId)
            if player then
                oper_act_64_packet_result_data(player, cfg, orderId, curOrderData, isFinishOrder)
            end
        end
    end
end

function oper_act_64_packet_result_data(sysarg, cfg, orderId, finishOrderData, isFinishOrder)
    local myActorId = Actor.getActorId(sysarg)

    local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_PLAYER].strName)

    if not rankPtr then
        return
    end

    local isOrderFinish = isFinishOrder
    if not isOrderFinish then
        isOrderFinish = 0
    end

    local playerNum = 0
    if finishOrderData.playerList then
        playerNum = #finishOrderData.playerList
    end
    local pack = oper_act_alloc_do_result_packet(sysarg, cfg.cmd_id, cfg.act_id, 0)
    if not pack then return end
    --print("isOrderFinish ="..isOrderFinish)
    DataPack.writeByte(pack, isOrderFinish)
    --print("orderId ="..orderId)
    DataPack.writeInt(pack, orderId)
    --print("finishOrderData.orderType ="..(finishOrderData.orderType or 0))
    DataPack.writeByte(pack, finishOrderData.orderType or 0)
    --print("playerNum ="..playerNum)
    DataPack.writeByte(pack, playerNum)
    if playerNum > 0 then
        for ii = 1, playerNum do
            local actorid = finishOrderData.playerList[ii]
            local playerItem = Ranking.getItemPtrFromId(rankPtr, actorid)
            if playerItem then
                local playerName = Ranking.getSub(playerItem, 0)
                local playerData = Ranking.getSub(playerItem, 1)
                local PlayerContentData = BaseFuc_unserialize(playerData)
                if not PlayerContentData then
                    PlayerContentData = {}
                end
                --print("actorid ="..actorid)
                DataPack.writeInt(pack, actorid or 0)
                --print("playerName ="..(playerName or ""))
                DataPack.writeString(pack, playerName or "")
                --print("PlayerContentData.vocation ="..(PlayerContentData.vocation or 0))
                DataPack.writeByte(pack, PlayerContentData.vocation or 0)
                --print("PlayerContentData.sex ="..(PlayerContentData.sex or 0))
                DataPack.writeByte(pack, PlayerContentData.sex or 0)
                local state = 0
                if finishOrderData.startPlayerId and finishOrderData.startPlayerId == actorid then
                    state = 1
                end
                --print("state ="..state)
                DataPack.writeByte(pack, state)
            end
        end
    end

    DataPack.flush(pack)
end

-- 跨天
function oper_act_64_new_day_arrive(act_id) 
     local ssVar = System.getStaticVar()
    if not ssVar.oper_act_64_start then
        return
    end
    if not ssVar.oper_act_64_cmd then
        return
    end
    if not ssVar.oper_act_64_act then
        return
    end
    
    local cfg = get_oper_act_cfg(ssVar.oper_act_64_cmd, ssVar.oper_act_64_act)
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end 

    local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_ORDERS].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local orderName = Ranking.getSub(item, 0)
            local data = Ranking.getSub(item, 1)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data) or {}
            end
          
            if dataTable.playerList and dataTable.orderType then
                local orderConfig = oper_act_64_get_order_config(config, dataTable.orderType)
                if orderConfig then
                    local playerNum = #dataTable.playerList
                    local discountConfig = oper_act_64_get_discount_config(orderConfig, playerNum)
                    if discountConfig then
                        for ii = 1, playerNum do
                            local actorid = dataTable.playerList[ii]
                            local playerData = gRankMgr_GetValueById(actorid, RANK_OPERATION_64_PLAYER, 1)
                            local myPlayerData = BaseFuc_unserialize(playerData)
                            if not myPlayerData then
                                myPlayerData = {}
                            end

                            local rewards = orderConfig.Awards
                            local title = Lang.ScriptTips.Operation174
                            local content = Lang.ScriptTips.Operation175
                            Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId064, Lang.LogTips.Operation064, myPlayerData.vocation, myPlayerData.sex)


                            if discountConfig.disCountNum and discountConfig.disCountNum > 0 then
                                local rewards = {{type = qatYuanbao, id = 0, count = discountConfig.disCountNum}}
                                local title = Lang.ScriptTips.Operation176
                                local content = Lang.ScriptTips.Operation177
                                Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId064, Lang.LogTips.Operation064, myPlayerData.vocation, myPlayerData.sex)
                            end
                        end
                    end
                end
            end
        end
    end
    

    gRankMgr_Clear(RANK_OPERATION_64_PLAYER)
    gRankMgr_Clear(RANK_OPERATION_64_ORDERS)

    ssVar.oper_act_64_cur_order = {}
end

function oper_act_64_get_order_config(config, orderType)
    if config.giftBagList and #config.giftBagList > 0 then
        for idx = 1, #config.giftBagList do
            local cfg = config.giftBagList[idx]
            if cfg and cfg.giftBagType == orderType then
                return cfg
            end
        end
    end
    return nil
end

function oper_act_64_get_discount_config(config, playerNum)
    if config.precentBack and #config.precentBack > 0 then
        for idx = 1, #config.precentBack do
            local cfg = config.precentBack[idx]
            if cfg and cfg.playerNum == playerNum then
                return cfg
            end
        end
    end
    return nil
end

function oper_act_64_get_my_order_list(sysarg, config)
    local myOrderList = {}
    local myActorId = Actor.getActorId(sysarg)
    local rankPtr = Ranking.getRanking(RankList[RANK_OPERATION_64_ORDERS].strName)
    if not rankPtr then 
        return myOrderList
    end
    local rankCount = Ranking.getRankItemCount(rankPtr)
    if rankCount > 0 then
        for idx = 1, rankCount do
            local item = Ranking.getItemFromIndex(rankPtr, idx-1)
            if item then
                local orderId = Ranking.getId(item)
                local data = Ranking.getSub(item, 1)
                local dataTable
                if not data or data == "" then
                    dataTable = {}
                else
                    dataTable = BaseFuc_unserialize(data) or {}
                end
              
                if dataTable.playerList and dataTable.orderType then
                    local orderConfig = oper_act_64_get_order_config(config, dataTable.orderType)
                    if orderConfig then
                        local playerNum = #dataTable.playerList
                        local discountConfig = oper_act_64_get_discount_config(orderConfig, playerNum)
                        if discountConfig then
                            for ii = 1, playerNum do
                                local actorid = dataTable.playerList[ii]
                                if actorid == myActorId then
                                    table.insert(myOrderList, orderId)
                                    break
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return myOrderList
end